package com.qinsong.myapplication.utils;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SaveLoginAndRegisterInfo {
    /**
     * 保存账号密码
     *
     * @param context
     * @param username
     * @param password
     * @return
     */
    public static boolean saveUserInfo(Context context, String username, String password) {
        try {
            FileOutputStream fos = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
            fos.write((username + ":" + password).getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }

    /**
     * 获取用户名密码
     * @param context
     * @return
     */
    public static Map<String,String> getUserInfo(Context context){
        String content = "";
        try {
            FileInputStream fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            Map<String,String> userMap = new HashMap<String ,String>();
            String [] infos = content.split(":");
            userMap.put("username",infos[0]);
            userMap.put("password",infos[1]);
            fis.close();
            return userMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
